package com.lemon.smartsafetynew.ui.adapter.patrol;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.listener.RecyclerViewMultipleItemClickListener;
import com.lemon.smartsafetynew.entity.patrol.SafePatrolCheckItemConclusionVo;
import com.lemon.smartsafetynew.entity.patrol.SafePatrolCheckItemResultBo;
import com.lemon.smartsafetynew.entity.patrol.SafePatrolCheckItemVo;

import java.util.ArrayList;
import java.util.List;

import kale.adapter.LemonRcvAdapter;
import kale.adapter.item.AdapterItem;

/**
 * Created by Andrew on 2017/8/2.
 */

public class PatrolCheckItemAdapter extends LemonRcvAdapter<SafePatrolCheckItemVo> {

    private RecyclerViewMultipleItemClickListener<SafePatrolCheckItemVo> mListener;

    public List<SafePatrolCheckItemResultBo> mResultList = new ArrayList<>();

    public PatrolCheckItemAdapter(@Nullable List<SafePatrolCheckItemVo> data,
                                  RecyclerViewMultipleItemClickListener<SafePatrolCheckItemVo> mListener) {
        super(data);
        this.mListener = mListener;
    }


    public List<SafePatrolCheckItemResultBo> getResultList() {
        return mResultList;
    }

    public void setResultList(List<SafePatrolCheckItemResultBo> mResultList) {
        this.mResultList = mResultList;
    }

    @NonNull
    @Override
    public AdapterItem createItem(Object type) {
        return new CheckItem();
    }


    private class CheckItem implements AdapterItem<SafePatrolCheckItemVo> {

        private TextView checkItemTitleTv;
        private TextView checkItemDescribeTv;

        private RadioButton successRadioBtn;
        private RadioButton failRadioBtn;

        private LinearLayout remarkLayout;
        private LinearLayout checkItemLayout;

        private SafePatrolCheckItemVo mData;
        private int mPosition;

        @Override
        public int getLayoutResId() {
            return R.layout.item_safe_patrol_check_item;
        }

        @Override
        public void bindViews(View root) {

            checkItemTitleTv = (TextView) root.findViewById(R.id.check_item_title_tv);
            checkItemDescribeTv = (TextView) root.findViewById(R.id.check_item_describe_tv);

            successRadioBtn = (RadioButton) root.findViewById(R.id.success_radio_btn);
            failRadioBtn = (RadioButton) root.findViewById(R.id.fail_radio_btn);

            remarkLayout = (LinearLayout) root.findViewById(R.id.remark_layout);
            checkItemLayout = (LinearLayout) root.findViewById(R.id.check_item_layout);

        }

        @Override
        public void setViews() {

            checkItemLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.onItemClick(mData, mPosition);
                }
            });

            remarkLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.onChildItemClick(mData, mPosition);
                }
            });

            successRadioBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.onPositiveItemClick(mData, mPosition);
                }
            });

            failRadioBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.onNegativeItemClick(mData, mPosition);
                }
            });

        }

        @Override
        public void handleData(SafePatrolCheckItemVo safePatrolCheckItemVo, int position) {

            mPosition = position;
            mData = safePatrolCheckItemVo;

            List<SafePatrolCheckItemConclusionVo> list = safePatrolCheckItemVo.getConclusions();

            checkItemTitleTv.setText(safePatrolCheckItemVo.getFlow_name());
            checkItemDescribeTv.setText(safePatrolCheckItemVo.getCheck_way());

            for(SafePatrolCheckItemConclusionVo checkItemConclusionVo : list) {

                if ("green_mark".equals(checkItemConclusionVo.getMark_code())) {
                    successRadioBtn.setText(checkItemConclusionVo.getConclusion_name());
                    successRadioBtn.setTag(checkItemConclusionVo.getFlow_id() + "," + checkItemConclusionVo.getConclusion_id());
                } else {
                    failRadioBtn.setText(checkItemConclusionVo.getConclusion_name());
                    failRadioBtn.setTag(checkItemConclusionVo.getFlow_id() + "," + checkItemConclusionVo.getConclusion_id());
                }
            }

            if(mResultList.get(position).isUserChoose()) {
                if(mResultList.get(position).getStatus() == 1) {
                    successRadioBtn.setChecked(true);
                } else {
                    failRadioBtn.setChecked(true);
                }
            } else {
                successRadioBtn.setChecked(false);
                failRadioBtn.setChecked(false);
            }

        }
    }
}
